define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'finance/settlement/index' + location.search,
                    add_url: 'finance/settlement/add',
                    edit_url: 'finance/settlement/edit',
                    del_url: 'finance/settlement/del',
                    multi_url: 'finance/settlement/multi',
                    import_url: 'finance/settlement/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: false,
                showSearch: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'),operate: false},
                        {field: 'hotel.hotel_name', title: __('Hotel.hotel_name'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'hotel_id', title: __('Hotel_id')},
                        // {field: 'company_id', title: __('Company_id')},
                        {field: 'order_job_date', title: __('Order_job_date'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'order_job_type', title: __('Order_job_type'), operate: false, searchList: {"1":__('Order_job_type 1'),"2":__('Order_job_type 2'),"3":__('Order_job_type 3'),"4":__('Order_job_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'time_hour', title: __('Time_hour'), operate: false},
                        {field: 'user.username', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user_first_time', title: __('小时工打卡记录'), operate: false, formatter: function (value,row,index){
                                return formatDate(value) + "<br />" + formatDate(row.user_second_time)
                        }},
                        {field: 'duration_hour', title: __('Duration_hour'), operate: false},
                        {field: 'user_first_location', title: __('小时工打卡地点'), operate: false, formatter: function (value,row,index){
                                return value + "<br />" + row.user_second_location
                            }},
                        {field: 'company_first_time', title: __('酒店打卡记录'), operate: false, formatter: function (value,row,index){
                                return formatDate(value) + "<br />" + formatDate(row.company_second_time)
                         }},
                        {field: 'company_hour', title: __('Company_hour'), operate: false},
                        {field: 'company_first_location', title: __('酒店打卡地点'), operate: false, formatter: function (value,row,index){
                                return value + "<br />" + row.company_second_location
                        }},
                        {field: 'fix_hour', title: __('Fix_hour'), operate: false},
                        {field: 'total_time', title: __('结算时长'), operate: false},

                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'all_price', title: __('All_price'), operate: false},
                        // {field: 'company.company_name', title: __('Company.company_name'), operate: 'LIKE'},
                        // {field: 'user_mobile', title: __('User_mobile'), operate: 'LIKE'},
                        // {field: 'user_first', title: __('User_first')},
                        // {field: 'user_second', title: __('User_second')},
                        // {field: 'user_second_location', title: __('User_second_location'), operate: 'LIKE'},
                        // {field: 'company_first', title: __('Company_first')},
                        // {field: 'compay_second', title: __('Compay_second')},
                        // {field: 'compay_second_location', title: __('Compay_second_location'), operate: 'LIKE'},
                        // {field: 'is_abnormal', title: __('Is_abnormal'), searchList: {"1":__('Is_abnormal 1'),"2":__('Is_abnormal 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'is_renewal', title: __('Is_renewal'), searchList: {"1":__('Is_renewal 1'),"2":__('Is_renewal 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"4":__('Status 4'),"5":__('Status 5'),"6":__('Status 6')}, formatter: Table.api.formatter.status},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            function formatDate(datetime) {
                if(datetime>1){
                    var date = new Date(datetime*1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
                    var year = date.getFullYear(),
                        month = ("0" + (date.getMonth() + 1)).slice(-2),
                        sdate = ("0" + date.getDate()).slice(-2),
                        hour = ("0" + date.getHours()).slice(-2),
                        minute = ("0" + date.getMinutes()).slice(-2),
                        second = ("0" + date.getSeconds()).slice(-2);
                    // 拼接
                    return year + "-"+ month +"-"+ sdate +" "+ hour +":"+ minute +":" + second;
                }
                return "-"
            }

            // 导出数据
            $(".btn-export").click(function (){
                window.location.href = 'settlement/export?' + $("form.form-commonsearch").serialize();
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});